655. 输出二叉树
655. 输出二叉树
Similar Question
leading to the advanced question
Solution Tips
方案一: DFS
var printTree = function(root) {
// 单独求解出高度, 构造好 matrix, 然后再次遍历填入比较合适
const calDepth = (root) => {
let h = 0;
if (root.left) {
h = Math.max(h, calDepth(root.left) + 1);
}
if (root.right) {
h = Math.max(h, calDepth(root.right) + 1);
}
return h;
}
const dfs = (res, root, r, c, height) => {
res[r][c] = root.val.toString();
if (root.left) {
dfs(res, root.left, r + 1, c - (1 << (height - r - 1)), height);
}
if (root.right) {
dfs(res, root.right, r + 1, c + (1 << (height - r - 1)), height);
}
}
const height = calDepth(root);
const m = height + 1;
const n = (1 << (height + 1)) - 1;
const res = new Array(m).fill(0).map(() => new Array(n).fill(''));
dfs(res, root, 0, Math.floor((n - 1) / 2), height);
return res;
};